<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>state</title>
  </head>
  <body>
    <div id="test"></div>
    <div id="test1"></div>
    <div id="test2"></div>

    <script
      src="https://unpkg.com/react@16/umd/react.development.js"
      crossorigin
    ></script>
    <script
      src="https://unpkg.com/react-dom@16/umd/react-dom.development.js"
      crossorigin
    ></script>
    <script src="https://unpkg.com/@babel/standalone/babel.min.js"></script>
    <!-- 引入prop-types，用于对组件标签属性进行限制 -->
    <script src="https://cdn.bootcdn.net/ajax/libs/prop-types/15.7.2/prop-types.js"></script>
    <script type="text/babel"></script>
  </body>
  <script type="text/babel">
    class Person extends React.Component {
      //构造器是否接收props，是否传递给super,取决于：是否希望在构造器中通过this访问props
      constructor(props) {
        console.log(props);
        super(props);
      }
      // state = { name: "tom", age: 18, sex: "女", age: 16 };
      render() {
        const { name, age, sex } = this.props;
        // this.props.name = "jack"; //此行代码辉报错，因为props是只读的
        return (
          <ul>
            <li>姓名:{name}</li>
            <li>性别:{sex}</li>
            <li>年龄:{age + 1}</li>
          </ul>
        );
      }
      //对标签属性进行类型、必要性的限制
      static propTypes = {
        name: PropTypes.string.isRequired, //限制name必传且为字符串
        sex: PropTypes.string, //限制性别为字符串
        age: PropTypes.number, //限制年龄为数字
      };
      //设置默认值
      static defaultProps = {
        sex: "男", //设置性别默认值
        age: 18, //限制年龄默认值
      };
    }

    //渲染组件
    ReactDOM.render(
      <Person name="tom" age={18} sex="男" />,
      document.getElementById("test")
    );
  </script>
</html>
